package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC436 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] findRightInterval(int[][] intervals) {
            TreeMap<Integer, int[]> map = new TreeMap<>();

            for (int i = 0; i < intervals.length; i++) {
                map.put(intervals[i][0], new int[]{intervals[i][1], i});
            }

            int[] res = new int[intervals.length];
            for (int intervalStart : map.keySet()) {
                int intervalEnd = map.get(intervalStart)[0];
                int idx = map.get(intervalStart)[1];
                Integer nextIntervalStart = map.ceilingKey(intervalEnd);
                if (null == nextIntervalStart) res[idx] = -1;
                else res[idx] = map.get(nextIntervalStart)[1];
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] arr = InputUtil.toDoubleIntegerArray("[[1,4],[2,3],[3,4]]");
        System.out.println(Arrays.toString(solution.findRightInterval(arr)));
    }

    public static void main(String[] args) throws Exception {
        LC436 an = new LC436();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
